package com.dense.kuiniu.bookborrow.business.adapter;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.business.entity.BorrowBook;
import com.dense.kuiniu.bookborrow.business.entity.QueryBookInfo;
import com.dense.kuiniu.bookborrow.utils.RandomCoverUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @date 2020/8/11
 */
public class BookBorrowOrReturnResultAdapter extends BaseQuickAdapter<QueryBookInfo, BaseViewHolder> {

    private boolean isBorrow;

    public BookBorrowOrReturnResultAdapter(int layoutResId, @Nullable List<QueryBookInfo> data, boolean isBorrow) {
        super(layoutResId, data);
        this.isBorrow = isBorrow;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder baseViewHolder, QueryBookInfo borrowBookBean) {
        ImageView cover = baseViewHolder.getView(R.id.iv_cover);
        Glide.with(getContext())
                .load(borrowBookBean.getBookCover())
                .centerCrop()
                .error(RandomCoverUtils.getRandomCover())
                .placeholder(RandomCoverUtils.getRandomCover())
                .into(cover);
        boolean isSucceed = "Y".equals(borrowBookBean.getBackFlag());
        if (isSucceed) {
            if (isBorrow) {
                borrowBookBean.setBorrowDate(TimeUtils.date2String(new Date(), "yyyy-MM-dd"));
                baseViewHolder.setText(R.id.tv_return_time, "应还日期：");
                String[] resultInfo = borrowBookBean.getBackInfor().split("应还日期");
                if (resultInfo.length == 2) {
                    baseViewHolder.setText(R.id.tv_return_time_value, resultInfo[1]);
                }
            } else {
                baseViewHolder.setText(R.id.tv_return_time, "归还日期：");
                baseViewHolder.setText(R.id.tv_return_time_value, TimeUtils.date2String(new Date(), "yyyy-MM-dd"));
            }
        } else {
            baseViewHolder.setText(R.id.tv_return_time_value, "-----");
        }

        baseViewHolder.setTextColor(R.id.tv_name, isSucceed ? Color.parseColor("#4D81FF") :
                Color.parseColor("#BFBFBF"));
        baseViewHolder.setText(R.id.tv_borrow_time_value, isSucceed ? borrowBookBean.getBorrowDate() :
                "-----");

        if (isBorrow) {
            if (isSucceed) {
                baseViewHolder.setBackgroundResource(R.id.iv_borrow_failed, R.mipmap.ic_borrow_succeed);
            } else {
                baseViewHolder.setBackgroundResource(R.id.iv_borrow_failed, R.mipmap.ic_borrow_failed);
            }
        } else {
            if (isSucceed) {
                baseViewHolder.setBackgroundResource(R.id.iv_borrow_failed, R.mipmap.ic_back_succeed);
            } else {
                baseViewHolder.setBackgroundResource(R.id.iv_borrow_failed, R.mipmap.ic_return_failed);
            }
        }
        if (isSucceed) {
            baseViewHolder.setText(R.id.tv_error_info, "");
        } else {
            baseViewHolder.setText(R.id.tv_error_info, borrowBookBean.getBackInfor());
        }
        baseViewHolder.setText(R.id.tv_name, String.format("《%s》", borrowBookBean.getName()));
        baseViewHolder.setText(R.id.tv_bar_code_value, borrowBookBean.getBookBarCode());
        ImageView tag = baseViewHolder.getView(R.id.iv_tag_periodical);
        tag.setVisibility(View.GONE);
    }

    public List<BorrowBook> getSucceedList() {
        List<BorrowBook> list = new ArrayList<>();
//        List<BorrowBook> all = getData();
//        for (BorrowBook book : all) {
//            if (book.getResultCode() == 0) {
//                list.add(book);
//            }
//        }
        return list;
    }

}